package ru.ipo.dces.client.messages;

import info.clearthought.layout.TableLayout;
import ru.ipo.dces.client.Controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PopupMessage extends JPanel {

    public static final int MESSAGE_WIDTH = 250;
    public static final int MESSAGE_HEIGHT = 100;
    public static final int FRAMES = 30;

    private Color background;
    private int bottomOffset;
    private int frame = 0;
    private final JPanel basePanel;
    private boolean deleted = false;
    private boolean drawn = false;

    /*private Timer timeToDelete = new Timer(2000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
//            PopupMessage.this.delete();
        }
    });*/

    private void delete() {
        deleted = true;
    }

    public PopupMessage(String message, PopupMessageType messageType, int bottomOffset, final JPanel basePanel) {
        this.basePanel = basePanel;
        this.bottomOffset = bottomOffset;

        if (messageType == PopupMessageType.Info)
            background = new Color(0, 255, 0, 100);
        if (messageType == PopupMessageType.GeneralServerError)
            background = new Color(255, 0, 0, 100);
        if (messageType == PopupMessageType.ServerReturnedError)
            background = new Color(255, 255, 0, 100);

        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.GRAY));
        setLayout(new TableLayout(
                new double[]{10, TableLayout.FILL, 10},
                new double[]{10, TableLayout.PREFERRED, 10}
        ));
        add(new JLabel("<html><p>" + message + "</p></html>"), "1, 1");

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e))
                    PopupMessage.this.delete();
            }

            /*@Override
            public void mouseEntered(MouseEvent e) {
                if (!hasDrawn())
                    return;
                timeToDelete.stop();
                setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
                basePanel.validate();
                basePanel.repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if (!hasDrawn())
                    return;
                timeToDelete.start();
                setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
                basePanel.validate();
                basePanel.repaint();
            }*/
        });
    }

    public boolean hasDrawn() {
        return drawn;
    }

    public void refresh() {
        setBounds(
                basePanel.getWidth() - MESSAGE_WIDTH,
                basePanel.getHeight() - Controller.getClientDialog().getStatusBar().getHeight()
                        - bottomOffset * MESSAGE_HEIGHT - MESSAGE_HEIGHT * frame / FRAMES,
                MESSAGE_WIDTH,
                MESSAGE_HEIGHT * frame / FRAMES
        );

        /*if (frame == FRAMES)
            if (!timeToDelete.isRunning())
                timeToDelete.start();*/

        if (frame < FRAMES)
            frame ++;
        else
            drawn = true;
        setVisible(true);
        validate();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(background);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public boolean isDeleted() {
        return deleted;
    }

    public int getBottomOffset() {
        return bottomOffset;
    }

    public void setBottomOffset(int offset) {
        bottomOffset = offset;
    }
}